package com.ccl.iot.privates;import java.nio.ByteBuffer;
import java.util.HashMap;

import android.graphics.Bitmap;

import com.ccl.iot.publics.Debugger;

public class BitmapCollector {
	/*
	 * 	Static functions
	 */
	private static BitmapCollector gCollector = new BitmapCollector();
	
	public static String GenerateID(Object aObject, int aWidth, int aHeight){
		if (aObject != null){
			return aObject.getClass().getSimpleName() + aWidth + "," + aHeight;
		}else{
			return aWidth + "," + aHeight;
		}
	}
	
	public static boolean Add(String aID, Bitmap aBitmap){
		return gCollector.add(aID, aBitmap);
	}
	
	public static Bitmap Get(String aID){
		return gCollector.get(aID);
	}
	
	public static boolean Remove(String aID){
		return gCollector.remove(aID);
	}
	
	public static boolean Contains(String aID){
		return gCollector.contains(aID);
	}
	
	public static void Clear(){
		gCollector.clear();
	}
	
	/*
	 * 	Bitmap buffer class
	 */
	private class BitmapBuffer{
		private int iWidth;
		private int iHeight;
//		private Config iConfig;
		private ByteBuffer iBuffer;
		
		public Bitmap bm;
		
		BitmapBuffer(Bitmap aBitmap){
			if (aBitmap != null){
				iWidth = aBitmap.getWidth();
				iHeight = aBitmap.getHeight();
//				iConfig = aBitmap.getConfig();
				bm = aBitmap;
//				aBitmap.getRowBytes()*aBitmap.getHeight();
				int rowBytes = aBitmap.getRowBytes();
				int height = aBitmap.getHeight();
				
//				if ((iBuffer = ByteBuffer.allocate(aBitmap.getByteCount())) != null){
				if ((iBuffer = ByteBuffer.allocate(height * rowBytes)) != null){
					aBitmap.copyPixelsToBuffer(iBuffer);
				}
			}
		}
		
		public boolean isValid(){
			return iWidth > 0 && iHeight > 0 && iBuffer != null;
		}
		
		public int getBufferSize(){
			if (iBuffer != null){
				return iBuffer.capacity();
			}
			
			return 0;
		}
		
		public Bitmap createBitmap(){
			if (isValid()){
				// TODO 这里会发生内存溢出
				/*Bitmap tBitmap = Bitmap.createBitmap(iWidth, iHeight, iConfig);
				
				iBuffer.position(0);
				tBitmap.copyPixelsFromBuffer(iBuffer);*/
				
				return bm;
			}
			
			return null;
		}
	}
	
	/*
	 * 	Non-static functions
	 */
	private long iMemoryUsage = 0;
	private HashMap<String, BitmapBuffer> iCollection = null;
	
	public boolean add(String aID, Bitmap aBitmap){
		if (aID != null && aBitmap != null && aBitmap.isRecycled() == false){
			if (iCollection == null){
				iCollection = new HashMap<String, BitmapBuffer>();
			}else{
				synchronized(iCollection){
					if (iCollection.containsKey(aID)){
						return true;
					}
				}
			}
			
			BitmapBuffer tBuffer = new BitmapBuffer(aBitmap);
			
			if (tBuffer.isValid()){
				iCollection.put(aID, tBuffer);
				
				iMemoryUsage += tBuffer.getBufferSize();
				Debugger.Debug("[" + iMemoryUsage + "]New bitmap [" + aID + "] add to collector");
				
				return true;
			}
		}
		
		return false;
	}
	
	public Bitmap get(String aID){
		if (aID != null && iCollection != null){
			synchronized(iCollection){
				BitmapBuffer tBuffer = iCollection.get(aID);
				
				if (tBuffer != null){
					return tBuffer.createBitmap();
				}
			}
		}
		
		return null;
	}
	
	public boolean remove(String aID){
		if (aID != null && iCollection != null){
			synchronized(iCollection){
				BitmapBuffer tBuffer = iCollection.remove(aID);
				
				if (tBuffer != null){
					iMemoryUsage -= tBuffer.getBufferSize();
					Debugger.Debug("[" + iMemoryUsage + "]Remove bitmap [" + aID + "] from collector");
				}
			}
		}
		
		return false;
	}
	
	public boolean contains(String aID){
		if (aID != null && iCollection != null){
			synchronized(iCollection){
				return iCollection.containsKey(aID);
			}
		}
		
		return false;
	}
	
	public void clear(){
		if (iCollection != null){
			iCollection.clear();
			iCollection = null;
		}
	}
}
